<?php

namespace App\Models;

use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;


/**
 * App\Models\Material
 *
 * @property int $id
 * @property int $uid
 * @property int $albumid 专辑ID，图片素材有效
 * @property string|null $name
 * @property string $source
 * @property string $thumb
 * @property string|null $width
 * @property string|null $height
 * @property string|null $type
 * @property string|null $extension 扩展名
 * @property int $size
 * @property int $views
 * @property int $downloads
 * @property mixed $created_at
 * @property mixed $updated_at
 * @property-read string $image
 * @property-read \App\Models\User $user
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material doc()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material file()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material filter($input = array(), $filter = null)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material image()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material paginateFilter($perPage = null, $columns = array(), $pageName = 'page', $page = null)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material simplePaginateFilter($perPage = null, $columns = array(), $pageName = 'page', $page = null)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material video()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material voice()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereAlbumid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereBeginsWith($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereDownloads($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereEndsWith($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereExtension($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereHeight($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereLike($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereSize($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereSource($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereThumb($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereViews($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Material whereWidth($value)
 * @mixin \Eloquent
 */
class Material extends Model
{
    use Filterable;

    protected $table = 'material';
    protected $primaryKey = 'id';
    protected $fillable = [
        'uid', 'albumid', 'name', 'source', 'thumb', 'width', 'height',
        'type', 'extension', 'size', 'views', 'downloads'
    ];

    public static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub

        static::creating(function (Material $material){
            if (!$material->uid) $material->uid = Auth::id();
        });

        static::deleted(function (Material $material) {
            @unlink(material_path(str_replace(material_url(), '', $material->thumb)));
            @unlink(material_path(str_replace(material_url(), '', $material->source)));
        });
    }

    /**
     * @param $value
     * @return string
     */
    public function getThumbAttribute($value)
    {
        return image_url($value);
    }

    /**
     * @param $value
     * @return string
     */
    public function getImageAttribute($value)
    {
        return image_url($value);
    }

    /**
     * @param $value
     */
    public function setThumbAttribute($value)
    {
        $this->attributes['thumb'] = str_replace(material_url(), '', $value);
    }

    /**
     * @param $value
     * @return string
     */
    public function getSourceAttribute($value)
    {
        return $value ? material_url($value) : $value;
    }

    /**
     * @param $value
     */
    public function setSourceAttribute($value)
    {
        $this->attributes['source'] = str_replace(material_url(), '', $value);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(User::class, 'uid', 'uid');
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeImage(Builder $query)
    {
        return $query->where('type', 'image');
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeVideo(Builder $query)
    {
        return $query->where('type', 'video');
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeVoice(Builder $query)
    {
        return $query->where('type', 'voice');
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeDoc(Builder $query)
    {
        return $query->where('type', 'doc');
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeFile(Builder $query)
    {
        return $query->where('type', 'file');
    }
}
